package org.dinnermate.db;

import java.sql.SQLException;
import java.util.Date;

import org.dinnermate.layout.Table;
import org.dinnermate.menu.Menu;
import org.dinnermate.menu.MenuItem;
import org.dinnermate.staff.Shift;
import org.dinnermate.staff.ShiftType;
import org.dinnermate.staff.StaffMember;

import sun.util.resources.CalendarData_ca;

import net.java.ao.EntityManager;

public class DBTest
{
	private EntityManager manager;

	public DBTest()
	{
		manager = DBManager.getEntityManager();
	}

	public StaffMember[] createUsers() throws SQLException
	{	
		StaffMember jeff, mike, liviu;

		// Create a new StaffMember
		// This will add a new row to the StaffMember table
		jeff = manager.create(StaffMember.class); // you can also call "DBManager.create(StaffMember.class)"

		// Set some of the fields
		jeff.setFullName("Jeff Wallace");
		jeff.setAddress("1234 Blank Street");
		jeff.setEmail("jeff@tjwallace.ca");
		jeff.setUserName("tjwallace");
		jeff.setPassword("password");
		jeff.setPhoneNumber("5141234567");
		jeff.setWage(30.00f);

		// Save the changes to the DB
		jeff.save();

		// same for mike
		mike = manager.create(StaffMember.class);
		mike.setFullName("Mike Kirk");
		mike.setUserName("mkirk"); // username is unique so we must set it
		mike.save();

		// same for liviu
		liviu = manager.create(StaffMember.class);
		liviu.setFullName("Liviu Chis");
		liviu.setUserName("lchis");
		liviu.save();

		StaffMember[] staff = { jeff, mike, liviu };

		return staff;
	}

	public ShiftType createShiftType( String name ) throws SQLException
	{
		ShiftType st = manager.create(ShiftType.class);
		st.setName(name);
		st.save();
		return st;
	}

	public Shift createShift( ShiftType type, StaffMember... mems ) throws SQLException
	{
		Shift shift = manager.create(Shift.class);
		shift.setType(type);
		shift.setDate(System.currentTimeMillis()/1000L);
		if (mems.length > 0) shift.setStaffMembers(mems);
		shift.save();
		return shift;
	}
	
	public void deleteStaffMembers( StaffMember... mems ) throws SQLException
	{
		manager.delete(mems);
	}

	public static void main( String[] args ) throws SQLException
	{
		DBTest t = new DBTest();
		StaffMember[] smems = t.createUsers();
		ShiftType type = t.createShiftType("Dinner");
		Shift shift = t.createShift(type, smems);
		
		StaffMember[] newSM = new StaffMember[2];
		newSM[0] = smems[0];
		newSM[1] = smems[1];
		shift.setStaffMembers(newSM);
		
		t.deleteStaffMembers(smems);
	}
}
